// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/

use serde::Deserialize; // 确保引入serde库以用于反序列化
use tauri::LogicalSize; // 确保引入tauri库以使用逻辑大小对象
use tauri::Size;
use tauri::Window; // 确保引入tauri库以使用窗口对象 // 确保引入tauri库以使用窗口大小对象

mod tray; // 引入tray模块以使用托盘功能

#[derive(Debug, Deserialize)]
struct DataType {
    width: i32,
    height: i32,
}

#[tauri::command]
fn greet(name: &str) -> String {
    println!("Hello, {}! You've been greeted from Rust!", name);
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[tauri::command]
fn testobject(width: i32, height: i32) -> String {
    println!("testobject: x:{},y:{}", width, height);
    format!("testobject: {}, field2: {}", width, height)
}

#[tauri::command]
fn set_win_size(window: Window, data: DataType) -> String {
    println!("set_win_size: x:{},y:{}", data.width, data.height);

    // 创建 LogicalSize 实例
    let logical_size = LogicalSize::new(data.width as f64, data.height as f64);
    // 手动创建 tauri::Size 的实例
    let size = Size::new(logical_size);
    // 设置窗口大小
    window.set_size(size).unwrap();

    format!("set_win_size: {}, field2: {}", data.width, data.height)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .setup(|app| {
            // 初始化托盘
            tray::create(app.handle()).unwrap();
            Ok(())
        })
        .plugin(tauri_plugin_shell::init())
        .invoke_handler(tauri::generate_handler![greet, testobject, set_win_size])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
